<?php

/**
 * 前台 前页面 模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: page.php 40 2011-07-11 17:15:06Z zwt007 $
 * http://www.t-mac.org；
 */
class aboutusAction extends Action
{

    private $tmp_model;
    private $show_model;
    private $index_model;

    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('news');
        $this->show_model = Tmac::model('show');
        $this->index_model = Tmac::model('index');
        $this->difang_model = Tmac::model('difang');

        $username = $this->H($this->getParam('username'));
        $this->userinfo = $this->difang_model->getUserinfo($username);

        $this->config = $this->index_model->getCommon($this->userinfo);
        
        $nav_array = $this->difang_model->getCategoryArray($this->userinfo['uid'], 0);

        $hotel_list_nopage_array = $this->show_model->getHotelListNoPage($this->userinfo['uid'], 5);
        //left hotel nopage
        $news_list_nopage_array = $this->show_model->getArticleNewsListNopage(8, $this->userinfo['uid']);
        //left 会议常识
        $hycs_cid = 70;
        $hycs_list_nopage_array = $this->show_model->getArticleNewsListNopageB($hycs_cid, $this->userinfo['uid']);

        $this->assign('hotel_list_nopage_array', $hotel_list_nopage_array);
        $this->assign('news_list_nopage_array', $news_list_nopage_array);
        $this->assign('hycs_list_nopage_array', $hycs_list_nopage_array);
        $this->assign('hycs_cid', $hycs_cid);
        $this->assign('nav_array', $nav_array);
        $this->assign('userinfo', $this->userinfo);        
    }

    public function index()
    {
        $cat_id = intval($this->getParam('cid'));
        $cid = empty($cat_id) ? 9 : $cat_id;
        $catinfo = $this->show_model->getCategoryInfoById($cid, $this->userinfo['uid']);

        //下级栏目        
        $url = 'aboutus';
        $parent = 2;    //父级id                
        $this->index_model->getCategoryList(9, $url, $this->userinfo['uid'], $this->userinfo['username'], $this->config['cfg_indexurl']);


        $now_url = '<a href="' . $this->config['cfg_indexurl'] . '">燕泰机构</a> > <a href="' . PHP_SELF . '?m=difang&username=' . $this->userinfo['username'] . '">燕泰' . $this->userinfo['nicename'] . '</a>';
        $now_url .= ' > <a href="' . PHP_SELF . '?m=' . $url . '&username=' . $this->userinfo['username'] . '">关于燕泰</a>';
        if (!empty($cid) && $cid <> 9) {
            $now_url.=' > ' . $catinfo['cat_name'];
        }

        $this->assign('now_url', $now_url);
        $this->assign('catinfo', $catinfo);
        $this->assign('cid', $cat_id);

        $this->V('page');
    }

    public function gbook()
    {
        $cat_id = intval($this->getParam('cid'));
        $cid = empty($cat_id) ? 9 : $cat_id;
        $catinfo = $this->show_model->getCategoryInfoById($cid, $this->userinfo['uid']);

        //下级栏目        
        $url = 'aboutus';
        $parent = 2;    //父级id                
        $this->index_model->getCategoryList(9, $url, $this->userinfo['uid'], $this->userinfo['username'], $this->config['cfg_indexurl']);


        $now_url = '<a href="' . $this->config['cfg_indexurl'] . '">燕泰机构</a> > <a href="' . PHP_SELF . '?m=difang&username=' . $this->userinfo['username'] . '">燕泰' . $this->userinfo['nicename'] . '</a>';
        $now_url .= ' > <a href="' . PHP_SELF . '?m=' . $url . '&username=' . $this->userinfo['username'] . '">关于燕泰</a>';
        if (!empty($cid) && $cid <> 9) {
            $now_url.=' > ' . $catinfo['cat_name'];
        }

        $this->assign('now_url', $now_url);
        $this->assign('catinfo', $catinfo);
        $this->assign('cid', $cat_id);

        $this->V('gbook');
    }

    public function gbook_save()
    {
        if (empty($_POST) || count($_POST) < 3) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $comment_title = trim($this->H($this->getParam('comment_title')));
        $comment_author = trim($this->H($this->getParam('comment_author')));
        $comment_author_tel = trim($this->H($this->getParam('comment_author_tel')));
        $comment_author_email = trim($this->H($this->getParam('comment_author_email')));
        $comment_content = trim($this->H($this->getParam('comment_content')));
        $yzm = intval($this->getParam('yzm'));

        if (trim(md5($yzm)) != trim($_SESSION['comment_valid'])) {
            $this->redirect("效验码输入错误!", PHP_SELF . "?m=comment");
            exit();
        }
        /* 验证不能为空 */
        if (empty($comment_title)) {
            $this->redirect('评论标题不能为空');
            exit;
        }

        if (!empty($comment_author_tel)) {
            if (!regExp::check_mobile($comment_author_tel)) {
                $this->redirect('手机号码格式不正确!');
                exit;
            }
        }
        if (!empty($comment_author_email)) {
            if (!regExp::Email($comment_author_email)) {
                $this->redirect('email格式不正确!');
                exit;
            }
        }
        if (!isset($comment_content)) {
            $this->redirect('评论内容不能为空!');
            exit;
        }
        if (empty($yzm)) {
            $this->redirect('验证码不能为空!');
            exit;
        }
        // TODO goon to verify
        $uid = $this->userinfo['uid'];
        
        $info = array(
            'comment_title' => $comment_title,
            'comment_author' => $comment_author,
            'comment_author_tel' => $comment_author_tel,
            'comment_author_email' => $comment_author_email,
            'comment_content' => $comment_content,
            'comment_time' => time(),
            'user_id' => $uid,
            'comment_author_ip' => $_SERVER["REMOTE_ADDR"]
        );
        //insert save article_class
        $rs = $this->tmp_model->addcomment($info);
        if ($rs) {
            $this->redirect('留言成功');
        } else {
            $this->redirect('留言失败');
        }
    }

}